package cn.copera.dyy.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.copera.dyy.common.web.BaseController;
import cn.copera.dyy.entity.PointPraiseSet;
import cn.copera.dyy.entity.Shop;
import cn.copera.dyy.entity.ShopImage;
import cn.copera.dyy.service.PointPraiseSetService;
import cn.copera.dyy.service.ShopImageService;
import cn.copera.dyy.service.ShopService;

/**
 * 分享Controller
 * 
 * 
 */
@Controller
@RequestMapping(value = "${adminPath}/share")
public class ShareController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	PointPraiseSetService praiseSetService;
	@Autowired
	private ShopService shopService;
	@Autowired
	ShopImageService shopImageService;

	// 开放
	@RequestMapping(value = "/enabled", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> enabled(
			@RequestParam(required = false) String shopId, String type) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"分享");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setType("分享");
				pointPraiseSet.setShop(shop);
				if (type.equals("1")) {
					pointPraiseSet.setEnabled("开放");
				} else {
					pointPraiseSet.setEnabled("关闭");
				}

				praiseSetService.save(pointPraiseSet);

			} else {
				if (type.equals("1")) {
					pointPraiseSet.setEnabled("开放");
				} else {
					pointPraiseSet.setEnabled("关闭");
				}
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("shareSet", pointPraiseSet);
			map.put("enabled", pointPraiseSet.getEnabled());
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}
	@RequestMapping(value="/shopimage",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> shopimage(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
		List<ShopImage> list =shopImageService.loadImage(shopId);
		if(list!=null&&list.size()>0){
			StringBuffer sb = new StringBuffer();
			for(ShopImage image : list){
				sb.append(","+image.getType());
			}
			map.put("types", sb.substring(1).toString());
		}else{
			map.put("types", "无");
		}
		map.put("list", list);
	    //改变上传完毕标志
//		Shop shop = shopService.get(shopId);
//		shop.setUploadFlag("no");
		//shopService.save(shop);
		return map;
	}
	@RequestMapping(value="/loadImage",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> loadImage(@RequestParam(required = false) String imageId){
		Map<String, Object> map = new HashMap<String,Object>();
		ShopImage image = shopImageService.getShopImage(imageId);
		
		map.put("image", image);
	   
		return map;
	}
	@RequestMapping(value="/saveDescripton",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveDescripton(@RequestParam(required = false) String shopImageId,String description){
		Map<String, Object> map = new HashMap<String,Object>();
		ShopImage si = shopImageService.getShopImage(shopImageId);
		si.setDescription(description);
		shopImageService.save(si);
		map.put("message", "success");
		return map;
	}
	@RequestMapping(value="/loadHeadImage",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> loadHeadImage(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
		Shop shop = shopService.get(shopId);
		map.put("headImage", shop.getHeadImage());
			
		return map;
	}
	@RequestMapping(value="/uploadFlag",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> uploadFlag(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
		Shop shop = shopService.get(shopId);
		map.put("uploadFlag", shop.getUploadFlag());
			
		return map;
	}
	@RequestMapping(value="/delimage",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delimage(@RequestParam(required = false)String imageId){
		Map<String, Object> map = new HashMap<String,Object>();
		shopImageService.removeImage(imageId);
		map.put("message", "success");
		
	
			
		return map;
	}
	// 累计分享数
	@RequestMapping(value = "/total", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> total(
			@RequestParam(required = false) String shopId, String totalCount) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"分享");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("分享");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setTotalCount(Integer.parseInt(totalCount
						.replace("次", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setTotalCount(Integer.parseInt(totalCount
						.replace("次", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("shareSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

	// 赠送点卡
	@RequestMapping(value = "/presenter", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> presenter(
			@RequestParam(required = false) String shopId, String totalCount) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"分享");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("分享");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setPresenterCount(Integer.parseInt(totalCount
						.replace("个", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setPresenterCount(Integer.parseInt(totalCount
						.replace("个", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("shareSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

	//每人每天最多分享
	@RequestMapping(value = "/day", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> day(
			@RequestParam(required = false) String shopId, String totalCount) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"分享");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("分享");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setDayCount(Integer.parseInt(totalCount
						.replace("次", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setDayCount(Integer.parseInt(totalCount
						.replace("次", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("shareSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

}
